import {Request, Response} from 'express'
import jwt from 'jsonwebtoken'
import Log4js from '../../../core/log/Log4jsConfig'
import {BaseController} from "../../../core/base/BaseController";
import {Controller} from "../../../core/decorator/Controller";
import {R} from "../../../core/domain/R";
import {RequestMapping, RequetMethod} from "../../../core/decorator/RequestMapping";
import {CommonConst} from "../../../core/consts/CommonConst";

const logger = Log4js.getLogger('AuthController')

@Controller('/auth')
export class AuthController extends BaseController {

    @RequestMapping('login', RequetMethod.POST)
    login(req: Request, res: Response) {
        logger.info('req body:', req.body)

        const authUser = {
            id: 1,
            username: 'admin'
        }

        const {username, password} = req.body

        if (username !== 'admin' || password !== 'admin') {
            res.json(R.fail('用户名或密码错误'))
        }

        const token = jwt.sign(authUser, CommonConst.authSecret, {
            expiresIn: CommonConst.authTokenExpires
        })

        const data = {
            token,
            userInfo: {
                name: 'admin',
                avatar: 'http://localhost:3005/avatar.jpg',
                uid: 'p1',
                email: 'admin@km.com',
                // 通知消息总数
                nofityCount: 12,
                // 未读消息总数
                unreadCount: 11
            },
            roles: ['admin'],

            menus: [{
                id: 1001,
                parentId: 0,
                title: 'Dashboard',
                path: '',
                icon: 'PieChartOutlined',
                children: [{
                    id: 1002,
                    parentId: 1001,
                    title: '分析页',
                    path: '/dashboard/analysis',
                    icon: ''
                }, {
                    id: 1003,
                    parentId: 1001,
                    title: '监控页',
                    path: '/dashboard/monitor',
                    icon: ''
                }, {
                    id: 1004,
                    parentId: 1001,
                    title: '工作台',
                    path: '/dashboard/workspace',
                    icon: ''
                }]
            }, {
                id: 1005,
                parentId: 0,
                title: '表单页',
                path: '',
                icon: 'FormOutlined',
                children: [{
                    id: 1006,
                    parentId: 1005,
                    title: '基础表单',
                    path: '/form/base-form',
                    icon: ''
                }, {
                    id: 1007,
                    parentId: 1005,
                    title: '分步表单',
                    path: '/form/step-form',
                    icon: ''
                }, {
                    id: 1008,
                    parentId: 1005,
                    title: '高级表单',
                    path: '/form/advanced-form',
                    icon: ''
                }]
            }, {
                id: 1009,
                parentId: 0,
                title: '列表页',
                path: '',
                icon: '',
                children: [{
                    id: 1010,
                    parentId: 1009,
                    title: '搜索列表',
                    path: '',
                    icon: '',
                    children: [{
                        id: 1011,
                        parentId: 1010,
                        title: '搜索列表(文章)',
                        path: '',
                        icon: ''
                    }, {
                        id: 1012,
                        parentId: 1010,
                        title: '搜索列表(项目)',
                        path: '',
                        icon: ''
                    }, {
                        id: 1013,
                        parentId: 1010,
                        title: '搜索列表(应用)',
                        path: '',
                        icon: ''
                    }]
                }, {
                    id: 1014,
                    parentId: 1009,
                    title: '查询表格',
                    path: '',
                    icon: ''
                }, {
                    id: 1015,
                    parentId: 1009,
                    title: '标准列表',
                    path: '',
                    icon: ''
                }, {
                    id: 1016,
                    parentId: 1009,
                    title: '卡片列表',
                    path: '',
                    icon: ''
                }]
            }, {
                id: 1017,
                parentId: 0,
                title: '详情页',
                path: '',
                icon: '',
                children: [{
                    id: 1018,
                    parentId: 1017,
                    title: '基础详情页',
                    path: '',
                    icon: ''
                }, {
                    id: 1019,
                    parentId: 1017,
                    title: '高级详情页',
                    path: '',
                    icon: ''
                }]
            }, {
                id: 1020,
                parentId: 0,
                title: '结果页',
                path: '',
                icon: '',
                children: [{
                    id: 1021,
                    parentId: 1020,
                    title: '成功页',
                    path: '',
                    icon: ''
                }, {
                    id: 1022,
                    parentId: 1020,
                    title: '失败页',
                    path: '',
                    icon: ''
                }]
            }, {
                id: 1023,
                parentId: 0,
                title: '异常页',
                path: '',
                icon: '',
                children: [{
                    id: 1024,
                    parentId: 1023,
                    title: '403',
                    path: '',
                    icon: ''
                }, {
                    id: 1025,
                    parentId: 1023,
                    title: '404',
                    path: '',
                    icon: ''
                }, {
                    id: 1026,
                    parentId: 1023,
                    title: '500',
                    path: '',
                    icon: ''
                }]
            }, {
                id: 1027,
                parentId: 0,
                title: '个人页',
                path: '',
                icon: '',
                children: [{
                    id: 1028,
                    parentId: 1027,
                    title: '个人中心',
                    path: '',
                    icon: ''
                }, {
                    id: 1029,
                    parentId: 1027,
                    title: '个人设置',
                    path: '',
                    icon: ''
                }]
            }, {
                id: 1030,
                parentId: 0,
                title: '图形编辑器',
                path: '',
                icon: '',
                children: [{
                    id: 1031,
                    parentId: 1030,
                    title: '流程编辑器',
                    path: '',
                    icon: ''
                }, {
                    id: 1032,
                    parentId: 1030,
                    title: '脑图编辑器',
                    path: '',
                    icon: ''
                }, {
                    id: 1033,
                    parentId: 1030,
                    title: '拓扑编辑器',
                    path: '',
                    icon: ''
                }]
            }]
        }
        res.json(R.ok(data))
    }
}
